package com.hkts.management.unattended.poundroom.service.serviceimpl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.management.unattended.order.entity.HktsOrderInfo;
import com.hkts.management.unattended.poundroom.entity.YbMutualCompany;
import com.hkts.management.unattended.poundroom.entity.YbMutualInventoryclass;
import com.hkts.management.unattended.poundroom.entity.YbMutualSovouch;
import com.hkts.management.unattended.poundroom.mapper.YbMutualCompanyMapper;
import com.hkts.management.unattended.poundroom.service.IYbMutualCompanyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zy
 * @since 2021-10-13
 */
@Service
@Transactional
@DS("slave")
public class YbMutualCompanySlaveServiceImpl extends ServiceImpl<YbMutualCompanyMapper, YbMutualCompany> {


    @Autowired
    private YbMutualCompanyMapper ybMutualCompanyMapper;

    public IPage<YbMutualCompany> selectPage(Page<YbMutualCompany> page, YbMutualCompany ybMutualCompany) {
        QueryWrapper<YbMutualCompany> query = Wrappers.query();
        QueryWrapper<YbMutualCompany> entityWrapper = getEntityWrapper(query, ybMutualCompany);
        return ybMutualCompanyMapper.selectPage(page, entityWrapper);
    }


    public List<YbMutualCompany> selectList(YbMutualCompany ybMutualCompany) {
        QueryWrapper<YbMutualCompany> query = Wrappers.query();
        QueryWrapper<YbMutualCompany> entityWrapper = getEntityWrapper(query, ybMutualCompany);
        return ybMutualCompanyMapper.selectList(entityWrapper);
    }

    /**
     * 公共查询条件
     *
     * @param entityWrapper
     * @return
     */
    public QueryWrapper<YbMutualCompany> getEntityWrapper(QueryWrapper<YbMutualCompany> entityWrapper, YbMutualCompany ybMutualCompany) {
        //条件拼接
        if (StringUtils.isNotBlank(ybMutualCompany.getcComName())) {
            entityWrapper.like(YbMutualCompany.CCOMNAME, ybMutualCompany.getcComName());
        }
        if (StringUtils.isNotBlank(ybMutualCompany.getcComType())) {
            entityWrapper.like(YbMutualCompany.CCOMTYPE, ybMutualCompany.getcComType());
        }
        if (StringUtils.isNotBlank(ybMutualCompany.getbDel())) {
            entityWrapper.like(YbMutualCompany.BDEL, ybMutualCompany.getbDel());
        }

        return entityWrapper;
    }
    public List<YbMutualCompany> getUpdateMutualCompany(LocalDateTime updateTime,int flag){
        QueryWrapper<YbMutualCompany> query = Wrappers.query();
        query.select("top 1000   AutoID,cComCode, cComName,code,name, cComType, bDel, doptTime, dcreateTime, dmodifyTime, ddeleteTime, u8optStatus, bmutualStatus");
        query.lambda().gt(updateTime!=null,YbMutualCompany::getDoptTime,updateTime);
        query.orderByAsc("AutoID");
        if(flag!=0){
            Integer count=flag*1000;
            Integer maxId=ybMutualCompanyMapper.getAutoIdMax(count);
            query.lambda().ge(YbMutualCompany::getAutoID,maxId);
        }
        return ybMutualCompanyMapper.selectList(query);
    }
    public Integer getUpdateTimeCount(LocalDateTime updateTime){
        QueryWrapper<YbMutualCompany> query = Wrappers.query();
        query.lambda().gt(updateTime!=null,YbMutualCompany::getDoptTime,updateTime);
        Integer count=ybMutualCompanyMapper.selectCount(query);
        double flag=count;
        double total=flag/1000;
        return (int)Math.ceil(total);
    }
    public Integer getBmutualStatusCount(){
        QueryWrapper<YbMutualCompany> query = Wrappers.query();
        query.lambda().eq(YbMutualCompany::getBmutualStatus,0);
        Integer count=ybMutualCompanyMapper.selectCount(query);
        double flag=count;
        double total=flag/1000;
        return (int)Math.ceil(total);
    }
    public List<YbMutualCompany> getBmutualStatuData(int flag){
        QueryWrapper<YbMutualCompany> query = Wrappers.query();
        query.select("top 1000   AutoID,cComCode, cComName,code,name, cComType, bDel, doptTime, dcreateTime, dmodifyTime, ddeleteTime, u8optStatus, bmutualStatus");
        query.lambda().eq(YbMutualCompany::getBmutualStatus,0);
        query.orderByAsc("AutoID");
        if(flag!=0){
            Integer count=flag*1000;
            Integer maxId=ybMutualCompanyMapper.getAutoIdMax(count);
            query.lambda().ge(YbMutualCompany::getAutoID,maxId);
        }
        return ybMutualCompanyMapper.selectList(query);
    }
}
